package com.magictower.primary.application;

import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.WindowManager;

/**
 * @author kejunzhan
 * @since 2018/1/24
 */

public class ScreenAdapterApplication extends Application {
    /**
     * DESIGN_WIDTH的值建议使用px换算dp的结果，即px/2
     */
    private static float DESIGN_WIDTH = 252f;

    private ScreenAdapterHelper mHelper;

    @Override
    public void onCreate() {
        super.onCreate();

        //init helper with the width for design drawing
        DisplayMetrics metrics = new DisplayMetrics();
        WindowManager WM = (WindowManager)this.getSystemService(Context.WINDOW_SERVICE);
        WM.getDefaultDisplay().getMetrics(metrics);

        //屏幕的分辨率
        int width = metrics.widthPixels;
        //int height = metrics.heightPixels;
        mHelper = new ScreenAdapterHelper.Builder()
                .setApplication(this)
                .setDesign_with(DESIGN_WIDTH)
                .setUnit(TypedValue.COMPLEX_UNIT_PT)
                .setEnableDp(false)
                .setEnableOtherResources(true)
                .build();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mHelper.onConfigurationChanged();
    }

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        //Will call before init
        if (mHelper != null)
            mHelper.getResources(res);
        return res;
    }
}

